package kmp;

public class leetCode151_reverseWords {
    public static void main(String[] args) {
        String s = "a good   example";
        leetCode151_reverseWords a = new leetCode151_reverseWords();
        a.reverseWords(s);
    }
    public String reverseWords(String s) {
        if(s == null || s.length() == 0) return s;
        String[] split = s.split(" ");
        int i = 0;
        int j = split.length - 1;
        while (i < j){
            String tmp = split[i];
            split[i] = split[j];
            split[j] = tmp;
            i++;
            j--;
        }
        StringBuilder sb = new StringBuilder();
        for (String s1 : split) {
            if(!s1.trim().equals("")) {
                sb.append(s1).append(" ");
            }
        }
        return sb.toString().trim();

    }

}
